<template>
	<view :class="size === 'small' ? 'small' : ''">
		<view class="uni-flex" :class="{unishadow :uniShadow}">
			<view class="uni-search-form uni-circular uni-background">
				<template  v-if="isSimple" >
					<view class="searchIco">
						<image src="/static/img/ico_search@3x.png" mode="aspectFit"></image>
						<text class="text">搜索</text>
					</view>
				</template>
				<template  v-else>
					<image src="/static/img/ico_search@3x.png"  class="uni-icon-position" mode="aspectFit"></image>
					<input
						type="text"
						:placeholder="placeholder"
						v-model.trim="inputVal"
						@input="onInput"
						@focus="onFocus"
						@confirm="confirm"
						placeholder-style="color:#B3A696;font-weight:bold"
					></input>
					<icon v-if="isShowClearIcon" type="clear" size="16" class="uni-icon-clear" @tap="clearInputValue"/>
				</template>
			</view>
			<view class="uni-action" v-if="isShowBtn"  @click="handleSearch">
				{{inputVal ? '搜索' : '取消'}}
			</view>
		</view>
		<view v-if="isShowSelect">
			<view class="uni-combox__selector" v-if="inputIsShow">
				<scroll-view scroll-y="true" class="uni-combox__selector-scroll">
					<view class="uni-combox__selector-empty " v-if="loading">
						<view>加载中··· </view>
					</view>
					<view class="uni-combox__selector-empty " v-if="!loading && dataListLength === 0" @tap="closeDielog">
						<view>{{emptyTips}}</view>
					</view>
					<view class="uni-combox__selector-item" v-for="(item,index) in dataList" :key="index" @tap="onSelectorClick(index)">
						<text>{{item[showValue]}}</text>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		name: 'select',
		props: {
			placeholder: {
				type: String,
				default: '输入关键字找找看'
			},
			infoList: {
				type: Array,
				default () {
					return []
				}
			},
			value: {
				type: String,
				default: ''
			},
			showValue: {
				type: String,
				default: 'name'
			},
			emptyTips: {
				type: String,
				default: '暂无数据'
			},
			loading: {
				type: Boolean,
				default: false
			},
			btnStyleColor: {
				type: Object,
				default() {
					return {}
				}
			},
			uniShadow: {
				type: Boolean,
				default: false
			},
			type: {
				type: String,
				default: 'primary'
			},
			size: {
				type: String,
				default: 'medium'
			},
			isShowSelect: {
				type: Boolean,
				default: false
			},
			isShowBtn: {
				type: Boolean,
				default: true
			},
			isSimple: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				inputIsShow: false,
				inputVal: '',
				dataList: [],
				isShowClearIcon: false
			}
		},
		computed: {
			dataListLength() {
				return this.dataList.length
			}
		},
		watch: {
			value: {
				handler(newVal) {
					this.inputVal = newVal
				},
				immediate: true
			},
			infoList: {
				handler(newVal) {
					this.dataList = newVal
				},
				deep: true,
				immediate: true
			},
			inputVal(val) {
				if(!val.length) {
					this.isShowClearIcon = false
				}
			}
		},
		methods: {
			closeDielog() {
				this.inputIsShow = false
			},
			onFocus() {
				if(this.dataList.length) {
					this.inputIsShow = true
					this.isShowClearIcon = !!this.inputVal
				}
			},
			onSelectorClick(index) {
				this.inputIsShow = false
				this.isShowClearIcon = false
				this.inputVal = this.dataList[index][this.showValue]
				this.$emit('input', this.inputVal)
				this.$emit('change', this.dataList[index])
			},
			onInput() {
				this.$emit('input', this.inputVal)
			},
			handleSearch() {
				this.inputIsShow = true
				this.$emit('handleSearch')
			},
			confirm() {
				this.$emit('confirm', this.inputVal)
			},
			clearInputValue() {
				this.inputVal = ''
			}
		}
	}
</script>
<style lang="scss" scoped>
	$selectWidth: 90%; // 下拉选择框宽度
	.small {
		transform: scale(.9, .9);
	}
	.uni-primary {
		background-color: $uni-color-primary;
	}
	.uni-success {
		background-color: #67c23a;
	}
	.uni-warning {
		background-color: $uni-color-warning;
	}
	.uni-error {
		background-color: $uni-color-error;
	}
	.hover{
		transition: all .6s;
		transform: scale(0.8,0.8);
	}
	.uni-flex{
		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 10upx 20upx;
		box-sizing: border-box;
	}
	.unishadow{
		box-shadow: 0upx 1upx 5upx #DDDDDD;
	}
	.uni-search-form{
		position: relative;
		width: 100%;
		margin: 10upx 0;
		padding: 10upx 80upx;
		font-size: 32upx;
		color: #999999;
		height: 60upx;
		input{
			height: 100%;
		}
		.searchIco{
			position: absolute;
			left: 50%;
			margin-left: -54upx;
			margin-top: 5upx;
			line-height: 30px;
			color: #664E37;
			image{
				will-change: transform;
				height: 34.6upx;
				width: 36upx;
				margin-top: -5upx;
			}
			.text{
				margin-top: -5upx;
				float: right;
				padding-left: 14upx;
				font-weight: bold;
			}
		}
	}
	.uniRound{
		border-radius: 5px;
	}
	.uni-circular{
		border-radius: 100upx;
	}
	.uni-icon-position{
		position: absolute;
		top: 50%;
		left: 26upx;
		height: 34.6upx;
		width: 36upx;
		transform: translate(0,-50%);
	}
	.uni-icon-clear{
		position: absolute;
		top: 50%;
		right: 26upx;
		transform: translate(0,-50%);
	}
	.uni-background{
		background-color: #FFFBF0;
	}
	/* button */
	.uni-action{
		width:140upx;
		height: 66upx;
		font-size: 32upx;
		line-height: 66upx;
		text-align: right;
		color: #664E37;
		font-weight: bold;
	}
	.uni-shadow-blur{
		box-shadow: 0upx 1upx 10upx #C8C7CC;
	}
	.uni-round{
		border-radius: 100upx;
	}
	.uni-combox__selector {
		position: absolute;
		top: 100upx;
		left: 40upx;
		box-sizing: border-box;
		width: $selectWidth;
		background-color: #FFFFFF;
		border-radius: 6px;
		box-shadow: #DDDDDD 4px 4px 8px, #DDDDDD -4px -4px 8px;
		z-index: 999;
	}
	.uni-combox__selector-scroll {
		max-height: 200px;
		box-sizing: border-box;
	}
	.uni-combox__selector::before {
		content: '';
		position: absolute;
		width: 0;
		height: 0;
		border-bottom: solid 6px #FFFFFF;
		border-right: solid 6px transparent;
		border-left: solid 6px transparent;
		left: 50%;
		top: -6px;
		margin-left: -6px;
	}
	.uni-combox__selector-empty{
		text-align: center;
		color: #8F8F94;
		padding: 20upx 0;
		font-size: 28upx;
	}
	.uni-combox__selector-item {
		/* #ifdef APP-NVUE */
		display: flex;
		/* #endif */
		font-size: 24upx;
		margin: 0px 10px;
		padding: 20upx 10upx;
		color: #808080;
	}
	.uni-combox__selector-item:hover {
		background-color: #DDDDDD;
	}
	.uni-combox__selector-empty:last-child,
	.uni-combox__selector-item:last-child {
		border-bottom: none;
	}
</style>
